package cn.zhagzheguo.xk.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.zhagzheguo.xk.dao.CourseDao;
import cn.zhagzheguo.xk.model.Course;
import cn.zhagzheguo.xk.status.CommonStatus;

@Repository
public class CourseDaoImpl implements CourseDao {

	@Autowired
	SqlSessionFactory sqlSessionFactory;

	@Override
	public Course getCourse(int id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			Course course = session.selectOne("cn.zhagzheguo.xk.mapper.CourseMapper.selectOne", id);
			return course;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Course> filteAll(Course course) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			System.out.println(course);
			List<Course> list = session.selectList("cn.zhagzheguo.xk.mapper.CourseMapper.filteAll", course);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public Course getCourseByCnoAndKxh(String cno, int kxh) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("cno", cno);
			map.put("kxh", kxh);
			Course c = session.selectOne("cn.zhagzheguo.xk.mapper.CourseMapper.selectOne_cnoAndkxh", map);
			return c;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Course> getByKeyword(String keyword, int id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("keyword", keyword);
			map.put("id", id);
			List<Course> list = session.selectList("cn.zhagzheguo.xk.mapper.CourseMapper.selectList_keyword", map);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Course> getByName(String name, int id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", name);
			map.put("id", id);
			List<Course> list = session.selectList("cn.zhagzheguo.xk.mapper.CourseMapper.selectList_name", map);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Course> getAllByPage(int id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			List<Course> list = session.selectList("cn.zhagzheguo.xk.mapper.CourseMapper.selectList", id);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int updateEnrolledNumber(int course_id, int enrolledNumber) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			Map<String, Object> map = new HashMap<>();
			map.put("id", course_id);
			map.put("enrolledNumber", enrolledNumber);

			session.update("cn.zhagzheguo.xk.mapper.CourseMapper.update_enrolledNumber", map);
			session.commit();
			return CommonStatus.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
			return CommonStatus.FAIL;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public static void main(String[] args) {
		// List<Course> courses = new CourseDaoImpl().getByKeyword("2", 0);
		// System.out.println(courses);
		new CourseDaoImpl().updateEnrolledNumber(44, 1);
	}
}
